package com.mcxx.modules.miniSalvation.fundpay.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.convert.FundPayConvert;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.CustomFundPayReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dao.write.CustomFundPayMemberWriteDao;
import com.mcxx.modules.miniSalvation.fundpay.dao.write.CustomFundPayWriteDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayExcelDto;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayExcelDto;
import com.mcxx.modules.miniSalvation.fundpay.entity.CustomFundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.CustomFundPayListSearchParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.CustomFundPayReadService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("customFundPayWriteService")
@TaTransactional
public class CustomFundPayWriteService extends BaseService {

    @Autowired
    private CustomFundPayWriteDao customFundPayWriteDao;
    @Autowired
    private CustomFundPayMemberWriteDao customFundPayMemberWriteDao;
    @Autowired
    private CustomFundPayReadService customFundPayReadService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;


    public int insert(CustomFundPayDTO customFundPayDTO){

        CustomFundPayEntity customFundPayEntity = new CustomFundPayEntity();

        customFundPayEntity.setStatus(Constant.FundPayState.CREATE);

        customFundPayEntity.setId(Constant.getUUID());

        customFundPayEntity.setPayTotal(0d);

        customFundPayEntity.setMemberNum(0);
        int rst = customFundPayWriteDao.insertIgnoreNull(customFundPayEntity);
        if(rst <= 0){
            throw new BizException("-1", "名册创建失败");
        }
        return  rst;
    }


    public int delCustomFundPay(List<String> customFundPayIdList){

        customFundPayMemberWriteDao.deleteByCustomFundPayIds(customFundPayIdList);


        int rst = customFundPayWriteDao.deleteByIds(customFundPayIdList);
        if(rst <= 0){
            return 0;
        }

        return 1;
    }


    public int updateStateByIds(List<String> customFundIdPayList, String state){
        return customFundPayWriteDao.updateStateByIds(customFundIdPayList, state);
    }


    public int updateAddMemberNum(String customFundPayId, Integer memberNum){
        int rst = customFundPayWriteDao.updateAddMemberNum(customFundPayId, memberNum);
        if(rst <= 0){
            throw new BizException(FailureCode.ERR_10041);
        }
        return rst;
    }


    public int updateSubMemberNum(String customFundPayId, Integer memberNum){
        int rst = customFundPayWriteDao.updateSubMemberNum(customFundPayId, memberNum);
        if(rst <= 0){
            throw new BizException("-1", "名册信息更新失败");
        }
        return  rst;
    }


    public int updatePayTotal(String customFundPayId){
        int rst = customFundPayWriteDao.updatePayTotal(customFundPayId);
        if(rst <= 0){
            throw new BizException("-1", "成员信息修改失败");
        }
        return rst;
    }


    public ExcelInfo exportExcelCustFundPay(String exportType, List<String> orderIdList, CustomFundPayListSearchParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        if(param.getPayDate() != null &&  param.getPayDate().length()<9){
            param.setPayDate(param.getPayDate() +"-01");
        }
        if("1".equals(exportType)){


            param.setIdsList(orderIdList);
            List<CustomFundPayExcelDto> list = customFundPayReadService.exportExcelCustFundPay(param);
            excelInfo = exportExcelService.exportOrgOpLogs(CustomFundPayExcelDto.class,list,"自定义发放名册信息.xls",
                    "自定义发放名册列表","自定义发放名册列表",false,"businessCode","payDate",
                    "itemTypes","payWays","commitDate");
        }

        return excelInfo;
    }
}
